var paginationTotal = 1;//记录总数
var paginationCurrentPage=1;
$(function(){
	$('[name=qco_status]').val(2);	//	初始化状态为 1
	initData(0); //初始化分页数据
    $('#dateFrom,#dateTo').datepicker({
		changeMonth: true,
		changeYear: true,			 
		dateFormat: 'yy-mm-dd'
	});
	$("#quanlityControlDetail").dialog({
		autoOpen:false,
		modal:true,
		width:850,
		show:"slide"
	});
	$('.qcDetail').live('click',function(){
		$.post('/merchant/quality-control/detail',{'qco_code':$(this).attr('num')},function(data){
			var html = '<div style="text-align:center;"><table id="qcDetail"><tr><td class="fontBold">QC Information</td><td class="fontBold">Qc Code</td><td>'+data.qco_code+'</td><td class="fontBold">ASN NO.</td><td>'+data.qco_ref_code+'</td></tr>';
			html+='<tr><td class="fontBold">Product Image</td><td class="fontBold">Qc Items</td><td class="fontBold">Qc Content</td><td class="fontBold">Unpass Quantity</td><td class="fontBold">Remark</td></tr>';
			$.each(data.product_info,function(key,val){
				$.each(val,function(k,v){
					if(k==0){ var bool=true;}
					var htmlTemp = '' ;
					$.each(v.proAttId,function(pk,pv){
						if(pv.pa_file_type=='img'){
						htmlTemp += '<div class="product-img-view" style="'+(pk!=0 ? 'display:none' : '')+'"><a class="fancybox-thumbs" data-fancybox-group="thumb" href="/merchant/product/view-image/fileName/'+pv.pa_path+'/productId/'+pv.product_id+'"><img style="width:100px;height:100px;" src="/merchant/product/view-image/fileName/'+pv.pa_path+'/productId/'+pv.product_id+'"></a></div>'
						}else{
							htmlTemp += '<div class="product-img-view" style="'+(pk!=0 ? 'display:none' : '')+'"><a class="fancybox-thumbs" data-fancybox-group="thumb" href="'+pv.pa_path+'"><img style="width:100px;height:100px;" src="'+pv.pa_path+'"></a></div>'	
						}	
					});
					
					html+='<tr>'+(bool ? '<td rowspan="'+(val.length+2)+'">'+htmlTemp+'</td>' : '')+'<td>'+v.poc_value+'</td><td>'+v.po_value+'</td><td>'+v.qcr_quantity_problem+'</td><td>'+v.qcr_description+'</td></tr>';
				});
				html+= '<tr><td class="fontBold">Product SKU</td><td class="fontBold">Product Weight</td><td class="fontBold">Send to QC Quantity</td><td class="fontBold">Unpass Quantity Throught QC</td></tr>';
				html += '<td>'+val[0].product_sku+'</td><td>'+val[0].product_weight+'</td><td>'+val[0].qci_quantity+'</td><td>'+val[0].qci_quantity_problem+'</td>';
			});
			html+= '</table></div>';
			$("#quanlityControlDetail").html(html);
			$("#quanlityControlDetail").dialog('open');
		},'json');
	});
});

function loadData(page,pageSize){
    var qc_code = $('[name=qc_code]').val();	
    var asn_code = $('[name=asn_code]').val();				
    var dateFrom = $('[name=dateFrom]').val();
    var dateTo = $('[name=dateTo]').val();
	var qco_status = $('[name=qco_status]').val();
	$.ajax({
		type: "POST",
		async: false,
		dataType: "json",
		url: "/merchant/quality-control/list",
		data:{
			'page':page,'pageSize':pageSize,
            'qc_code':qc_code,'asn_code':asn_code,'dateFrom':dateFrom,'dateTo':dateTo,
            'qco_status':qco_status
		},
		success: function(json) {
			//clear();
			var headerHtml = '';
			headerHtml += '<div class="cell" style="width:15%">QC Code</div>';
			headerHtml += '<div class="cell" style="width:20%">ASN No</div>';
			headerHtml += '<div class="cell" style="width:15%">Check Quantity</div>';
			headerHtml += '<div class="cell" style="width:15%">Pass Quantity</div>';
			headerHtml += '<div class="cell" style="width:18%">Problem Quantity</div>';
			headerHtml += '<div class="cell" style="width:7%">Staus</div>';
			headerHtml += '<div class="cell no-background" style="width:10%">Action</div>';
			$(".row-header").html(headerHtml);
			var html = "";
            paginationTotal = json.total;
			if(json.ask != 1) {
				html = '<div style="padding:5px; text-align:center;">No Data Returns</div>';
			}else {
				$.each(json.result,function(k,v){
					var st = v.qco_status;
					if(st == 0) st = 'Draft';
					else if(st==1) st='Confirm';
					else if(st==2) st='Finish';
					else if(st==3) st='Problem';
					else if(st==4) st='Delete';
					html+= '<div class="row height25">';
					html+= '<div class="cell" style="width:15%">'+v.qco_code+'</div>';	
					html+= '<div class="cell" style="width:20%">'+v.qco_ref_code+'</div>';
					html+= '<div class="cell" style="width:15%">'+v.qco_quantity+'</div>';
					html+= '<div class="cell" style="width:15%">'+v.qco_quantity_pass+'</div>';
					html+= '<div class="cell" style="width:18%">'+v.qco_quantity_problem+'</div>';
					html+= '<div class="cell" style="width:7%">'+st+'</div>';
					html+= '<div class="cell no-background" style="width:10%"><img src="/images/icon_view.gif" class="cursor qcDetail" num="'+v.qco_code+'" style="margin-top:5px;" title="Detail"/></div>';	
					html+= '</div>';
				});
			}
			$("#receiving-list").html(html);	
			
			$("#status0").html(json.qcostatusTotal.draftTotal);
			$("#status1").html(json.qcostatusTotal.confirmTotal);
			$("#status2").html(json.qcostatusTotal.finishTotal);
			$("#status3").html(json.qcostatusTotal.problemTotal);
			$("#status4").html(json.qcostatusTotal.deleteTotal);
            if(json.tabselect!=undefined && json.tabselect ){
               $("#status"+json.tabselect).parent("li").addClass('tab-li-click');
            }
		}
	});
}

function tabSelect(obj,status){
    $('[name=qco_status]').val(status);
    $(obj).addClass('tab-li-click');
    $(obj).siblings().removeClass('tab-li-click');
    paginationCurrentPage=1;
    initData(0);
}

function qcSearch() {
    $('[name=qco_status]').val('');
    $('.tab-li').removeClass('tab-li-click');
    paginationCurrentPage=1;
    initData(0);
}

function clear(){
	$('[name=qc_code]').val('');	
    $('[name=asn_code]').val('');			
    $('[name=qco_status]').val('');
    $('[name=dateFrom]').val('');
    $('[name=dateTo]').val('');
}